var express = require('express');
var app = express();

app.use((req, res, next) => {
  res.append('Access-Control-Allow-Origin', '*')
  res.append('Access-Control-Allow-Headers', 'Content-Type, Authorization')
  res.append('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE')
  setTimeout(next, 500 + Math.random()*1000)
})


let interfaces = [
  {
    method: 'POST',
    url: '/api/user/token',
    request: {
      "username": "",
      "password": ""
    },
    response: {
      "message": "获取成功",
      "status": 1,
      "token": "eyJhbGciOiJIUzI1NiIsImV4cCI6MTUxNjc4Mjg4NywiaWF0IjoxNTE2Nzc5ODg3fQ.eyJpZCI6IjIzMjQ0MzczNTAifQ.m7WXsa7FvAnBnP6YgzpkzvaLRi6KTqkURPdg0SWO0Ic"
    }
  },
  {
    method: 'GET',
    url: '/api/info/cet/zkzh',
    request: {},
    response: {
      "ID": "360012171105504",
      "name": "罗经宇",
      // "school": "南昌大学",
      // "score": "464.00",
      // "listening": "140",
      // "reading": "171",
      // "writing": "153",
      // "oralId": "F17136001031816",
      // "oralLevel": "C+"
    }
  },
  {
    method: 'GET',
    url: '/api/info/neeacet',
    request: {},
    response: {
      "img": "http://cet.neea.edu.cn/imgs/28b60c846d1e41218e117529c433dd2b.png",
      "status": 1,
      "msg": "done",
      "result_cookies": "580962314.39455.0000"
    }
  },
  {
    method: 'POST',
    url: '/api/info/neeacet',
    request: {
      "key": "3nec",
      "cet_num": "360012171105504",
      "result_cookies": "2745223178.39455.0000"
    },
    response: {
      "data": {
        "ID": "360012171105504",
        "name": "罗经宇",
        "school": "南昌大学",
        "score": "464.00",
        "t": "0",
        "listening": "140",
        "reading": "171",
        "writing": "153",
        "oralId": "F17136001031816",
        "oralLevel": "C+",
        "exam_type": "CET4"
      },
      "msg": "查询成功",
      "status": 1
    }
  }
]

interfaces.map(itf => {
  if(itf.method == 'POST') {
    app.post(itf.url, (req, res) => {
      res.send(itf.response)
    })
  } else if(itf.method == 'GET') {
    app.get(itf.url, (req, res) => {
      res.send(itf.response)
    })
  }
})

var server = app.listen(3001, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});